﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace POC.Reports.Document
{
    public static  class ExportExtn
    {
        public static DataTable ToTable(this List<object> messages, string [] columns=null)
        {
            object topRow = messages.FirstOrDefault();
            if (topRow == null) return new DataTable();
            DataTable dt = topRow.ToTableSchema(columns);
            foreach (var message in messages)
            {
                var row = message.ToTableRow(dt, columns);
                dt.Rows.Add(row);
            }
            return dt;
        }
        public static string ToXml(this DataTable dt)
        {
            return dt.ToXml();
        }
        public static void ToFile(this DataTable dt, string filePath)
        {
             dt.WriteXml(filePath, XmlWriteMode.WriteSchema);
        }
       
        private static string GetExcelColumnName(int columnNumber)
        {
            int dividend = columnNumber;
            string columnName = String.Empty;
            int modulo;

            while (dividend > 0)
            {
                modulo = (dividend - 1) % 26;
                columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
                dividend = (int)((dividend - modulo) / 26);
            }

            return columnName;
        }
       
        public static string ConvertToHtmlFile(this DataTable targetTable)
        {
            string myHtmlFile = "";



            if (targetTable == null)
            {
                throw new System.ArgumentNullException("targetTable");
            }
            else
            {
                //Continue.
            }



            //Get a worker object.
            StringBuilder myBuilder = new StringBuilder();



            //Open tags and write the top portion.
            myBuilder.Append("<html xmlns='http://www.w3.org/1999/xhtml'>");
            myBuilder.Append("<head>");
            myBuilder.Append("<title>");
            myBuilder.Append("Page-");
            myBuilder.Append(Guid.NewGuid().ToString());
            myBuilder.Append("</title>");
            myBuilder.Append("</head>");
            myBuilder.Append("<body>");
            myBuilder.Append("<table border='1px' cellpadding='5' cellspacing='0' ");
            myBuilder.Append("style='border: solid 1px Silver; font-size: x-small;'>");



            //Add the headings row.



            myBuilder.Append("<tr align='left' valign='top'>");



            foreach (DataColumn myColumn in targetTable.Columns)
            {
                myBuilder.Append("<td align='left' valign='top'>");
                myBuilder.Append(myColumn.ColumnName);
                myBuilder.Append("</td>");
            }



            myBuilder.Append("</tr>");



            //Add the data rows.
            foreach (DataRow myRow in targetTable.Rows)
            {
                myBuilder.Append("<tr align='left' valign='top'>");



                foreach (DataColumn myColumn in targetTable.Columns)
                {
                    myBuilder.Append("<td align='left' valign='top'>");
                    myBuilder.Append(myRow[myColumn.ColumnName].ToString());
                    myBuilder.Append("</td>");
                }



                myBuilder.Append("</tr>");
            }



            //Close tags.
            myBuilder.Append("</table>");
            myBuilder.Append("</body>");
            myBuilder.Append("</html>");



            //Get the string for return.
            myHtmlFile = myBuilder.ToString();



            return myHtmlFile;
        }
        /*
        public static void Build(DataSet ds)
        {
            StringWriter sw = new StringWriter();
            HtmlTextWriter w = new HtmlTextWriter(sw);

            foreach (DataTable dt in ds.Tables)
            {
                //Create a table
                Table tbl = new Table();

                //Create column header row
                TableHeaderRow thr = new TableHeaderRow();
                foreach (DataColumn col in dt.Columns)
                {
                    TableHeaderCell th = new TableHeaderCell();
                    th.Text = col.Caption;
                    thr.Controls.Add(th);
                }
                tbl.Controls.Add(thr);

                //Create table rows
                foreach (DataRow row in dt.Rows)
                {
                    TableRow tr = new TableRow();
                    foreach (var value in row.ItemArray)
                    {
                        TableCell td = new TableCell();
                        td.Text = value.ToString();
                        tr.Controls.Add(td);
                    }
                    tbl.Controls.Add(tr);
                }

                tbl.RenderControl(w);

            }

            //Response.Write(sw.ToString());
        }
        */

    }
}
